home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / database / postgres / mpsql-1.001 / mpsql-1~ / mpsql-1.0 / mquel.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-29  |  14.7 KB  |  438 lines

  1. /************************************************************************/
  2. /* Module : mquel.c                                    */
  3. /* Purpose: motif SQL / query tool for Postgres95                    */
  4. /* By     : Keith R. Davis                                */
  5. /* Date   : 12/8/95                                    */
  6. /* Notes  : Copyright(c) 1996 White River Software                    */
  7. /************************************************************************/
  8.  
  9. #include <Xm/Xm.h>        /* motif lib header        */
  10. #include <Xm/Protocols.h>       /* motif protocol header        */
  11. #include <Xm/PushB.h>        /* push button widget header    */
  12. #include <Xm/MainW.h>        /* main window widget header    */
  13. #include <Xm/PanedW.h>        /* paned window widget header    */
  14. #include <Xm/ScrolledW.h>       /* scrolled window widget head. */
  15. #include <Xm/Form.h>        /* form widget header            */
  16. #include <Xm/Frame.h>           /* frame widget header          */
  17. #include <Xm/Label.h>        /* label widget header        */
  18. #include <Xm/Text.h>        /* text widget header        */
  19. #include <stdio.h>              /* stdio header                 */ 
  20. #include <stdlib.h>             /* stdlib header                */
  21. #include <unistd.h>             /* unix std header              */
  22.  
  23. #include "image.h"              /* image header                 */
  24. #include "pixmap.h"             /* button bar icons             */
  25. #include "mquel.h"              /* mquel header                 */
  26. #include "callback.h"           /* callback header              */
  27. #include "menu.h"        /* menu header                */
  28. #include "llist.h"              /* linked list header           */
  29. #include "util.h"               /* util function header         */
  30. #include "db.h"                 /* db header                    */
  31.  
  32. /* global widget handle */
  33. MQUELwidgets AppWidgets;
  34. MQUELwidgets *AppWidgetsPtr = &AppWidgets;
  35.  
  36. /* Main: entry point */
  37. void main (int argc, char **argv)
  38. {    
  39.     XtAppContext   app;                            /* application context        */
  40.     int           status;                    /* function return status        */
  41.     Atom           WM_DELETE_WINDOW;                /* delete window atom               */
  42.  
  43.     /* setup the shell widget */
  44.     AppWidgets.shell = XtVaAppInitialize(&app, "Mpsql", NULL, 0,
  45.                      &argc, argv, NULL,
  46.                      XmNdeleteResponse, XmDESTROY,
  47.                      NULL);
  48.  
  49.     /* register wm protocol callback func. */
  50.     WM_DELETE_WINDOW = XInternAtom(XtDisplay(AppWidgets.shell),
  51.                    "WM_DELETE_WINDOW", FALSE);
  52.  
  53.     /* add callback for protocol */
  54.     XmAddWMProtocolCallback(AppWidgets.shell, WM_DELETE_WINDOW,
  55.                 DeleteWindowCallback, NULL);
  56.  
  57.     
  58.     /* setup the mainwindow widget */
  59.     AppWidgets.mainwindow = XtCreateManagedWidget("main",xmMainWindowWidgetClass,
  60.                           AppWidgets.shell, NULL, 0);
  61.     /* setup the button bar widget */
  62.     AppWidgets.btnbar = XtCreateManagedWidget("btnbar", xmFormWidgetClass, 
  63.                           AppWidgets.mainwindow, NULL, 0);
  64.  
  65.      /* setup the new file btn */
  66.     AppWidgets.btn_new = XtVaCreateManagedWidget("btnnew",
  67.                          xmPushButtonWidgetClass,
  68.                          AppWidgets.btnbar,
  69.                          XmNtopAttachment, XmATTACH_FORM,
  70.                          XmNtopOffset, 5,
  71.                          XmNbottomAttachment, XmATTACH_NONE,
  72.                          XmNleftAttachment, XmATTACH_FORM,
  73.                          XmNleftOffset, 5,
  74.                          XmNrightAttachment, XmATTACH_NONE,
  75.                          NULL);
  76.     /* set button image */
  77.     InstallLabeledPixmap(AppWidgets.btn_new, new_xpm);
  78.  
  79.     /* add callback for the new btn */
  80.     XtAddCallback(AppWidgets.btn_new, XmNactivateCallback,
  81.           NewCallback, NULL);
  82.    
  83.     /* setup the open file btn */
  84.     AppWidgets.btn_open = XtVaCreateManagedWidget("btnopen",
  85.                           xmPushButtonWidgetClass,
  86.                           AppWidgets.btnbar,
  87.                           XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
  88.                           XmNtopWidget, AppWidgets.btn_new,
  89.                           XmNbottomAttachment,
  90.                           XmATTACH_OPPOSITE_WIDGET,
  91.                           XmNbottomWidget, AppWidgets.btn_new,
  92.                           XmNleftAttachment, XmATTACH_WIDGET,
  93.                           XmNleftWidget, AppWidgets.btn_new,
  94.                           XmNrightAttachment, XmATTACH_NONE, 
  95.                           NULL);
  96.     /* set button image */
  97.     InstallLabeledPixmap(AppWidgets.btn_open, open_xpm);
  98.  
  99.      /* add callback for open file btn */
  100.     XtAddCallback(AppWidgets.btn_open, XmNactivateCallback,
  101.           OpenCallback, NULL);
  102.  
  103.  
  104.     /* setup the save file btn */
  105.     AppWidgets.btn_save = XtVaCreateManagedWidget("btnsave",
  106.                             xmPushButtonWidgetClass,
  107.                             AppWidgets.btnbar,
  108.                             XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
  109.                             XmNtopWidget, AppWidgets.btn_new,
  110.                             XmNbottomAttachment,
  111.                             XmATTACH_OPPOSITE_WIDGET,
  112.                             XmNbottomWidget, AppWidgets.btn_new,
  113.                             XmNleftAttachment, XmATTACH_WIDGET,
  114.                             XmNleftWidget, AppWidgets.btn_open,
  115.                             XmNrightAttachment, XmATTACH_NONE, 
  116.                             NULL);
  117.     /* set button image */
  118.     InstallLabeledPixmap(AppWidgets.btn_save, save_xpm);
  119.  
  120.     /* add callback for the save btn */
  121.     XtAddCallback(AppWidgets.btn_save, XmNactivateCallback,
  122.           SaveCallback, NULL);
  123.  
  124.     /* setup the print file btn */
  125.     AppWidgets.btn_print = XtVaCreateManagedWidget("btnprint",
  126.                             xmPushButtonWidgetClass,
  127.                             AppWidgets.btnbar,
  128.                             XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
  129.                             XmNtopWidget, AppWidgets.btn_new,
  130.                             XmNbottomAttachment,
  131.                             XmATTACH_OPPOSITE_WIDGET,
  132.                             XmNbottomWidget, AppWidgets.btn_new,
  133.                             XmNleftAttachment, XmATTACH_WIDGET,
  134.                             XmNleftWidget, AppWidgets.btn_save,
  135.                             XmNrightAttachment, XmATTACH_NONE, 
  136.                             NULL);
  137.     /* set button image */
  138.     InstallLabeledPixmap(AppWidgets.btn_print, print_xpm);
  139.  
  140.     /* add callback for the print btn */
  141.     XtAddCallback(AppWidgets.btn_print, XmNactivateCallback,
  142.           PrintCallback, NULL);
  143.  
  144.  
  145.     /* setup the execute SQL btn */
  146.     AppWidgets.btn_exe = XtVaCreateManagedWidget("btnexe",
  147.                             xmPushButtonWidgetClass,
  148.                             AppWidgets.btnbar,
  149.                             XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
  150.                             XmNtopWidget, AppWidgets.btn_new,
  151.                             XmNbottomAttachment,
  152.                             XmATTACH_OPPOSITE_WIDGET,
  153.                             XmNbottomWidget, AppWidgets.btn_new,
  154.                             XmNleftAttachment, XmATTACH_WIDGET,
  155.                             XmNleftWidget, AppWidgets.btn_print,
  156.                             XmNrightAttachment, XmATTACH_NONE, 
  157.                             NULL);
  158.     /* set button image */
  159.     InstallLabeledPixmap(AppWidgets.btn_exe, execute_xpm);
  160.  
  161.     /* add callback for execute SQL btn */
  162.     XtAddCallback(AppWidgets.btn_exe, XmNactivateCallback,
  163.           ExecSQLCallback, NULL);
  164.  
  165.  
  166.     /* setup the spool results btn */
  167.     AppWidgets.btn_spool = XtVaCreateManagedWidget("btnspool",
  168.                             xmPushButtonWidgetClass,
  169.                             AppWidgets.btnbar,
  170.                             XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
  171.                             XmNtopWidget, AppWidgets.btn_new,
  172.                             XmNbottomAttachment,
  173.                             XmATTACH_OPPOSITE_WIDGET,
  174.                             XmNbottomWidget, AppWidgets.btn_new,
  175.                             XmNleftAttachment, XmATTACH_WIDGET,
  176.                             XmNleftWidget, AppWidgets.btn_exe,
  177.                             XmNrightAttachment, XmATTACH_NONE, 
  178.                             NULL);
  179.     /* set button image */
  180.     InstallLabeledPixmap(AppWidgets.btn_spool, spool_xpm);
  181.  
  182.     /* add callback for spoll SQL btn */
  183.     XtAddCallback(AppWidgets.btn_spool, XmNactivateCallback,
  184.           SpoolCallback, NULL);
  185.  
  186.     /* setup the db connect btn */
  187.     AppWidgets.btn_connect = XtVaCreateManagedWidget("btnconnect",
  188.                             xmPushButtonWidgetClass,
  189.                             AppWidgets.btnbar,
  190.                             XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
  191.                             XmNtopWidget, AppWidgets.btn_new,
  192.                             XmNbottomAttachment,
  193.                             XmATTACH_OPPOSITE_WIDGET,
  194.                             XmNbottomWidget, AppWidgets.btn_new,
  195.                             XmNleftAttachment, XmATTACH_WIDGET,
  196.                             XmNleftWidget, AppWidgets.btn_spool,
  197.                             XmNrightAttachment, XmATTACH_NONE, 
  198.                             NULL);
  199.     /* set button image */
  200.     InstallLabeledPixmap(AppWidgets.btn_connect, connect_xpm);
  201.  
  202.     /* add callback for the connect btn */
  203.     XtAddCallback(AppWidgets.btn_connect, XmNactivateCallback,
  204.           ConnectCallback, NULL);
  205.  
  206.     /* setup the paned widget */
  207.     AppWidgets.pane = XtVaCreateManagedWidget("pane",
  208.                           xmPanedWindowWidgetClass,
  209.                           AppWidgets.mainwindow,
  210.                           XmNsashHeight, 8,
  211.                           NULL);
  212.  
  213.     /* setup the SQL window */
  214.     AppWidgets.sqlwindow = XmCreateScrolledText(AppWidgets.pane, "sqlwindow", NULL, 0);
  215.     XtManageChild(AppWidgets.sqlwindow);
  216.     XtVaSetValues(AppWidgets.sqlwindow,
  217.           XmNeditMode, XmMULTI_LINE_EDIT,
  218.           NULL);
  219.  
  220.     /* add callback for the SQL window */
  221.     XtAddCallback(AppWidgets.sqlwindow, XmNmodifyVerifyCallback,
  222.           TextModifiedCallback, NULL);
  223.     
  224.     /* setup the result window */
  225.     AppWidgets.resultwindow = XmCreateScrolledText(AppWidgets.pane, "resultwindow", NULL, 0);
  226.     XtManageChild(AppWidgets.resultwindow);
  227.     XtVaSetValues(AppWidgets.resultwindow,
  228.           XmNautoShowCursorPosition, FALSE,
  229.           XmNcursorPositionVisible, FALSE,
  230.           XmNeditMode, XmMULTI_LINE_EDIT,
  231.           XmNeditable, FALSE,
  232.           NULL);
  233.    
  234.     /* create the menu */
  235.     AppWidgets.menu = CreateMenuBar(AppWidgets.mainwindow);
  236.  
  237.     /* create the message label's frame */
  238.     AppWidgets.msgframe = XtVaCreateManagedWidget("msgframe",
  239.                           xmFrameWidgetClass,
  240.                           AppWidgets.mainwindow,
  241.                           XmNshadowType, XmSHADOW_ETCHED_IN,
  242.                           NULL);
  243.  
  244.     /* create the message window */
  245.     AppWidgets.message = XtVaCreateManagedWidget("message",
  246.                          xmLabelWidgetClass,
  247.                          AppWidgets.msgframe,
  248.                          XmNmarginHeight, 4,
  249.                          XmNmarginWidth, 4,
  250.                          XmNalignment, XmALIGNMENT_BEGINNING,
  251.                          NULL);
  252.  
  253.     /* set work areas for the main window */
  254.     XtVaSetValues(AppWidgets.mainwindow,
  255.           XmNmenuBar, AppWidgets.menu,
  256.           XmNcommandWindow, AppWidgets.btnbar,
  257.           XmNworkWindow, AppWidgets.pane,
  258.           XmNmessageWindow, AppWidgets.msgframe,
  259.           XmNshowSeparator, FALSE,
  260.           NULL);
  261.  
  262.     /* set app's icon */
  263.     SetupIcon(AppWidgets.shell, icon_xpm);
  264.  
  265.     /* get saved app options */
  266.     MQUEL_GetOptions();
  267.  
  268.     /* initialize the buffer list */
  269.     if(LLIST_Init() == NULL){
  270.       fprintf(stdout, "Error: Out of memory.\n");
  271.       exit(-1);
  272.     }
  273.  
  274.     /* add the scratch buffer to the buffer list */
  275.     if((buffer_curr = DB_OpenScratchBuffer()) == NULL){
  276.       fprintf(stdout, "Error: Out of memory.\n"); 
  277.       LLIST_Remove();
  278.       exit(-1);
  279.     }
  280.  
  281.     buffer_curr = DB_SetBuffer(buffer_curr);
  282.  
  283.     /* start the application */
  284.     XtRealizeWidget(AppWidgets.shell);
  285.  
  286.     /* process cmd line */
  287.     if(MQUEL_GetCmdLineArg(argc, argv))
  288.       DB_Connect();
  289.  
  290.     XtAppMainLoop(app);
  291. }
  292.  
  293. /************************************************************************/
  294. /* Function: MQUEL_GetOptions                                           */
  295. /* Purpose : gets app option from setup file                            */
  296. /* Params  :                                                            */
  297. /* Returns : 1 on SUCCESS / 0 on FAILURE                                */
  298. /* Notes   :                                                            */
  299. /************************************************************************/
  300.  
  301. int MQUEL_GetOptions(void)
  302. {
  303.   FILE *optf;
  304.   int col_sep;
  305.   char home[MAX_PATH_LEN];    
  306.   char db_msg[ERR_MSG_SIZE];  
  307.   int toggle_state;
  308.  
  309.   strcat(strcpy(home, getenv("HOME")), "/.mpsql");
  310.  
  311.   if((optf = fopen(home, "r")) != NULL){
  312.     fscanf(optf, "%s\n",&dbname);
  313.     fscanf(optf, "%s\n",&host);
  314.     fscanf(optf, "%s\n",&port);
  315.     fscanf(optf, "%s\n",&printer);
  316.     fscanf(optf, "%s\n",&spool_file);
  317.     fscanf(optf, "%d\n",&col_width);
  318.     fscanf(optf, "%d\n",&col_sep);
  319.     SetColSeparator(col_sep);
  320.     fscanf(optf, "%d\n",&toggle_state);
  321.     XmToggleButtonSetState(AppWidgetsPtr->tglspool, toggle_state, TRUE);
  322.     fscanf(optf, "%d\n",&toggle_state);
  323.     XmToggleButtonSetState(AppWidgetsPtr->tglcol, toggle_state, TRUE);
  324.     fscanf(optf, "%d\n",&toggle_state);
  325.     XmToggleButtonSetState(AppWidgetsPtr->tglecho, toggle_state, TRUE);
  326.   
  327.     if(fclose(optf) != 0){
  328.       sprintf(db_msg, "Error closing options file:\n%s", home);
  329.       ErrorMsg("Save Options", db_msg);
  330.       return 0;
  331.     }
  332.     return 1;
  333.   }
  334.   else{
  335.    strcpy(dbname, "postgres");
  336.    strcpy(host, "localhost");
  337.    strcpy(port, "5432");
  338.    strcpy(printer, "lpr");
  339.    strcpy(spool_file, "/tmp/spool.txt");
  340.    SetColSeparator(NONE);
  341.    col_width = 12;
  342.    return 0;
  343.   }
  344. }
  345.  
  346. /************************************************************************/
  347. /* Function: MQUEL_SaveOptions                                          */
  348. /* Purpose : saves app options to setup file                            */
  349. /* Params  :                                                            */
  350. /* Returns : 1 on SUCCESS / 0 on FAILURE                                */
  351. /* Notes   :                                                            */
  352. /************************************************************************/
  353.  
  354. int MQUEL_SaveOptions(void)
  355. {
  356.   FILE *optf;
  357.   char home[MAX_PATH_LEN];  
  358.   char db_msg[ERR_MSG_SIZE];   
  359.  
  360.   strcat(strcpy(home, getenv("HOME")), "/.mpsql");
  361.  
  362.   if((optf = fopen(home, "w+")) == NULL){
  363.     sprintf(db_msg, "Error opening options file:\n%s", home);
  364.     ErrorMsg("Save Options", db_msg);
  365.     return 0;
  366.   }
  367.  
  368.   DB_Grab();
  369.  
  370.   fprintf(optf, "%s\n", dbname);
  371.   fprintf(optf, "%s\n", host, optf);
  372.   fprintf(optf, "%s\n", port, optf);
  373.   fprintf(optf, "%s\n", printer, optf);
  374.   fprintf(optf, "%s\n", spool_file, optf);
  375.   fprintf(optf, "%d\n", col_width);
  376.   fprintf(optf, "%d\n", GetColSeparatorId());
  377.   fprintf(optf, "%d\n", XmToggleButtonGetState(AppWidgetsPtr->tglspool));
  378.   fprintf(optf, "%d\n", XmToggleButtonGetState(AppWidgetsPtr->tglcol));
  379.   fprintf(optf, "%d\n", XmToggleButtonGetState(AppWidgetsPtr->tglecho));
  380.  
  381.   fflush(optf);
  382.  
  383.   DB_UnGrab();
  384.  
  385.   if(fclose(optf) != 0){
  386.     sprintf(db_msg, "Error closing options file:\n%s", home);
  387.     ErrorMsg("Save Options", db_msg);
  388.     return 0;
  389.   } 
  390.   sprintf(db_msg, "Saved file: %s", home);
  391.   XtVaSetValues(AppWidgetsPtr->message,
  392.         XtVaTypedArg, XmNlabelString, XmRString,
  393.         db_msg, strlen(db_msg)+1, NULL);
  394.   return 1;
  395. }
  396.  
  397. /************************************************************************/
  398. /* Function: MQUEL_GetCmdLineArg                                        */
  399. /* Purpose : gets the command line args                                 */
  400. /* Params  : argc : arg count                                           */
  401. /*           argv : array of cmd line args                              */
  402. /* Returns : 1 if dbname passed) / 0 on no dbname passed                */
  403. /* Notes   :                                                            */
  404. /************************************************************************/
  405.  
  406. int MQUEL_GetCmdLineArg(int argc, char **argv)
  407. {
  408.   extern char* optarg;
  409.   extern int optind, opterr;
  410.   int c;
  411.   int db = 0;
  412.  
  413.   while((c = getopt(argc, argv, "D:H:P:h")) != EOF){
  414.     switch(c){
  415.     case 'D':
  416.       strcpy(dbname, optarg);
  417.       db = 1;
  418.       break;
  419.     case 'H':
  420.       strcpy(host, optarg);
  421.       break;
  422.     case 'P':
  423.       strcpy(port, optarg);
  424.       break;
  425.     case 'h':
  426.       fprintf(stdout, "usage: mpsql -D[database name] -H[host] -P[port] -h[help]\n");
  427.       exit(0);
  428.     default:
  429.       exit(-1);
  430.     }
  431.   }
  432.  
  433.   if(db)
  434.     return 1;
  435.   else
  436.     return 0;
  437. }
  438.